package com.touchealth.test.cainiao;

import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse;
import org.apache.http.NameValuePair;
import org.apache.http.client.HttpClient;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.net.URI;
import java.net.URL;
import java.security.MessageDigest;
import java.util.*;

/**
 * @author zhangyanqiang
 * @date 2021/1/27
 **/
public class CaiNiao {

    /**物流账号账号加密
     * @param content
     * @param charset
     * @param keys
     * @return
     */
    public static String doSign(String content, String charset, String keys) {
        String sign = "";
        content = content + keys;
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(content.getBytes(charset));
            sign = new String(Base64.encodeBase64(md.digest()), charset);
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
        return sign;
    }

    public static String routeQuery() throws Exception {
        String appName=null;
        String resourceCode=null;
        String secretKey=null;
        String routeTbsandboxUrl = null;
        String routeOnlineUrl = null;
        String extention =null;
        secretKey="JO4T7Ap6ji50PT9d1rK7awoD03eGfkWu";
        routeTbsandboxUrl = "https://link.tbsandbox.com/gateway/link.do\t\n";
        routeOnlineUrl = "http://link.cainiao.com/gateway/link.do\t";
        extention = null;
        String logistics_interface="{\n" +
                "\"appName\":\"476095\",\n" +
                "\"cpCode\":\"YTO\",\n" +
                "\"mailNo\":\"YT2121216191315\n" +
                "}";
        String charset="utf-8";
        String logistic_provider_id ="70fa076ed284cfeb0dbd782c4a60ab18";
        String msg_type="LPC_PACK_PUB_QUERY";
        String keys=secretKey;
        String to_code="LD-PACKPUSH";
        String md5=doSign(logistics_interface,charset,keys);
        Map<String,String> map=new HashMap<String,String>();
        map.put("logistics_interface",logistics_interface);
        map.put("logistic_provider_id",logistic_provider_id);
        map.put("msg_type",msg_type);
        map.put("data_digest",md5);
        map.put("to_code",to_code);

        HttpClient httpClient = null;
        HttpPost httpPost = null;
        String result = null;
        String url=routeOnlineUrl;
        try{
            httpClient =  HttpClients.createDefault();

            URL url1 = new URL(url);
            URI uri = new URI(url1.getProtocol(), url1.getHost(), url1.getPath(), url1.getQuery(), null);
            httpPost = new HttpPost(uri);
            //设置参数
            List<NameValuePair> list = new ArrayList<NameValuePair>();
            Iterator iterator = map.entrySet().iterator();

            while(iterator.hasNext()){
                Map.Entry<String,String> elem = (Map.Entry<String, String>) iterator.next();
                list.add(new BasicNameValuePair(elem.getKey(),elem.getValue()));
            }
            if(list.size() > 0){
                UrlEncodedFormEntity entity = new UrlEncodedFormEntity(list,charset);
                httpPost.setEntity(entity);
            }
            HttpResponse response = httpClient.execute(httpPost);
            System.out.print("route result:{}"+response);
            if(response != null){
                HttpEntity resEntity = response.getEntity();
                System.out.print("route result2:{}"+resEntity);
                if(resEntity != null){
                    result = EntityUtils.toString(resEntity,charset);
                }
                System.out.print("route result3:{}"+result);
            }
        }catch(Exception ex){
            System.out.print("e:{}"+ex);
        }
        return result;

    }
    public static void main(String[] args) throws Exception {
        routeQuery();

    }
}
